home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / d3d.zip / TRAFO.C < prev   
Text File  |  1989-11-24  |  2KB  |  80 lines

  1. /* TRAFO: Three-dimensional transformations  */
  2.  
  3. #include <math.h>
  4.  
  5. double   r11, r12, r13, r21, r22, r23, 
  6.          r31, r32, r33, r41, r42, r43;
  7.  
  8. void  initrotate( double a1, double a2, double a3,
  9.                   double v1, double v2, double v3, double alpha)
  10.  
  11. /* Computation of the rotation matrix
  12.  
  13.              | r11  r12  r13  0 |
  14.              | r21  r22  r23  0 |
  15.         R =  | r31  r32  r33  0 |
  16.              | r41  r42  r43  1 |
  17.  
  18.    to be used as [x1  y1  z1  1] = [x  y  z  1] R,
  19.    see function 'rotate'.
  20.    Point (x1, y1, z1) is the image of (x, y, z).
  21.    The rotation takes place about the axis
  22.       (a1, a3, a3) + lambda(v1, v2, v3)
  23.    and through the angle alpha.                 */
  24.  
  25. {
  26.    double rho, theta, cal, sal, cph, sph, cth, sth,
  27.         cph2, sph2, cth2, sth2, pi, cal1;
  28.  
  29.    cal = cos(alpha);
  30.    sal = sin(alpha);
  31.    cal1 = 1.0 - cal;
  32.    rho = sqrt(v1*v1 + v2*v2 + v3*v3);
  33.    pi = 4.0 * atan(1.0);
  34.    if(rho  == 0.0) {
  35.       theta = 0.0;
  36.       cph   = 1.0;
  37.       sph   = 0.0;
  38.    }
  39.    else {
  40.         if (v1 == 0.0)
  41.          theta = (v2 >= 0.0 ? 0.5*pi : 1.5 * pi);
  42.       else {
  43.          theta = atan(v2/v1);
  44.             if (v1 < 0)
  45.             theta += pi;
  46.       }
  47.         cph = v3/rho;
  48.       sph = sqrt(1.0 - cph*cph);
  49.       /* cph = cos(phi), sph = sin (phi) */
  50.    }
  51.    cth = cos(theta); 
  52.    sth = sin(theta);
  53.    cph2 = cph*cph; 
  54.    sph2 = 1.0 - cph2;
  55.    cth2 = cth*cth; 
  56.    sth2 = 1.0 - cth2;
  57.  
  58.    r11 = (cal*cph2 + sph2)*cth2 + cal*sth2;
  59.    r12 = sal*cph + cal1*sph2*cth*sth;
  60.    r13 = sph*(cph*cth*cal1 - sal*sth);
  61.    r21 = sph2*cth*sth*cal1 - sal*cph;
  62.    r22 = sth2*(cal*cph2 + sph2) + cal*cth2;
  63.    r23 = sph*(cph*sth*cal1 + sal*cth);
  64.    r31 = sph*(cph*cth*cal1 + sal*sth);
  65.    r32 = sph*(cph*sth*cal1 - sal*cth);
  66.    r32 = sph*(cph*sth*cal1 - sal*cth);
  67.    r33 = cal*sph2 + cph2;
  68.    r41 = a1- a1*r11 - a2*r21 - a3*r31;
  69.    r42 = a2- a1*r12 - a2*r22 - a3*r32;
  70.    r43 = a3- a1*r13 - a2*r23 - a3*r33;
  71. }        
  72.  
  73. void rotate(double x, double y, double z,
  74.       double *px1, double *py1, double *pz1)
  75. {
  76.    *px1 = x*r11 + y*r21 + z*r31 + r41;
  77.    *py1 = x*r12 + y*r22 + z*r32 + r42;
  78.    *pz1 = x*r13 + y*r23 + z*r33 + r43;
  79. }
  80.